home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: overloading new operator
- Message-ID: <1996Feb20.112926.1780@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 20 Feb 96 11:29:26 WET
- References: <marnoldDn0A8r.Ipu@netcom.com>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <marnoldDn0A8r.Ipu@netcom.com> marnold@netcom.com (Matt Arnold)
- writes:
-
- [snip: explanation of `placement new']
-
- > This form allows you to write code like this...
- >
- > char my_memory[100];
- >
- > new(my_memory) MyObject;
- >
-
- Please note that this is not guaranteed to work. Apart from making sure we
- provide enough memory for an instance of the MyObject class, we must
- guarantee the memory is suitably aligned:
-
- union Align { // will work on (probably) all architectures
- char c;
- short s;
- int i;
- long l;
- float f;
- double d;
- long double ld;
- void *p;
- void (*pf)();
- };
-
- void f()
- {
- union {
- Align a;
- char buf[sizeof(MyObject)];
- } my_memory;
-
- new (&my_memory) MyObject;
- }
-
- - Wil
-